<template>
  <div style="margin-top: 60px;margin-left:330px;width: 300px;height: 500px;border: 0px solid red;" >
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="ID">
        <el-input v-model="ruleForm.id" readonly></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="ruleForm.nickname"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>

      <el-form-item>
        <el-radio v-model="ruleForm.status" label="禁用">禁用</el-radio>
        <el-radio v-model="ruleForm.status" label="启用">启用</el-radio>
      </el-form-item>

      <el-form-item>
        <el-radio v-model="ruleForm.userRole" label="ROLE_ADMIN">管理员</el-radio>
        <el-radio v-model="ruleForm.userRole" label="ROLE_USER">普通用户</el-radio>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>


export default {
  name:"UserUpdate",
  data() {
    return {
      category:null,
      ruleForm: {
        nickname: '',
        password: '',
        id: 0,
        status:'',
        email: '',
        userRole:''
      },
      userList: '',
      rules: {
        nickname: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        birthday: [
          { required: true, message: '请选择生日', trigger: 'change' }
        ],
        password: [
          {
            required:true,message:'请输入密码',trigger:'blur'
          },
        ],
        balance: [
          {
            required:true,message:'请输入余额',trigger:'blur'
          }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      const _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.id = Number(this.ruleForm.id);
          axios.post('http://localhost:8082/manager/updateUser',_this.ruleForm).then(function (resp) {

            if(resp.data.code === 200){
              _this.$alert(_this.ruleForm.nickname+'修改成功', '', {
                confirmButtonText: '确定',
                callback: action => {
                  _this.$router.push('/UserManager')
                }
              });

            }else {
              _this.$alert(_this.ruleForm.name+resp.data.message, '', {
                confirmButtonText: '确定',

              });
            }
          })
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    const _this = this

    axios.get('http://localhost:8082/manager/findById/'+_this.$route.query.id).then(function (resp) {
      _this.ruleForm = resp.data.data
    }).catch(error=>{
      if (error.response) {
        // 请求发送成功，但服务器返回错误状态码
        console.log('Error status:', error.response.status);
        console.log('Error data:', error.response.data);
      } else if (error.request) {
        // 请求发送失败
        console.log('Request error:', error.request);
      } else {
        // 其他类型的错误
        console.log('Error message:', error.message);
      }

    })
  }
}
</script>
